package com.woniuxy.orderservice.rabbit.consumer;

import com.woniuxy.core.status.OrderState;
import com.woniuxy.core.status.TeamResevState;
import com.woniuxy.core.util.DateUtil;
import com.woniuxy.orderclient.client.ProductFeignClient;
import com.woniuxy.orderdao.model.Order;
import com.woniuxy.orderdao.model.OrderLog;
import com.woniuxy.orderservice.service.OrderService;
import com.woniuxy.util.MyAlipayUtil;
import com.woniuxy.util.PayInfoDto;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class TeamOrderConsumer {
    @Autowired
    private MyAlipayUtil myAlipayUtil;
    @Autowired
    private OrderService orderService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ProductFeignClient pfc;
    @RabbitHandler
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "team-order-consumer"),
                    exchange = @Exchange(type = "direct", value = "team-order-dead-ex"),
                    key = "team-order-dead-key"
            )
    })
    public void teamOrderConsumerExecute(String orderNum) throws Exception{
        //如果订单状态是已支付
        Order orderInfo = orderService.getById(orderNum);
        if (orderInfo.getOrderState() == OrderState.IS_PAID.getState()){
            //生成进度
            rabbitTemplate.convertAndSend("team-order-process-queue",orderNum);
            return;
        }
        if (orderInfo.getOrderState()== OrderState.NOT_PAY.getState()){//未支付的状态
            PayInfoDto payInfoDto = myAlipayUtil.selectPayInfo(orderNum);
            String payDate = payInfoDto.getPayDate();
            String payState = payInfoDto.getPayState();
            if ("TRADE_SUCCESS".equals(payState)){ // 判断支付状态是否是已支付，如果是则改变票务状态为已购买1，订单状态为已支付1
                //改订单状态
                orderInfo.setOrderState(OrderState.IS_PAID.getState());
                orderInfo.setOrderPayTime(payDate);
                orderService.updateById(orderInfo);
                //生成进度
                rabbitTemplate.convertAndSend("team-order-process-queue",orderNum);
                //记录日志
                OrderLog log = new OrderLog();
                log.setLogUser(orderInfo.getUserName()+":"+orderInfo.getUserPhone());
                log.setLogDesc("用户支付团教课程订单");
                log.setLogTime(DateUtil.dateTimeToString(new Date()));
                log.setOrderNum(orderNum);
                rabbitTemplate.convertAndSend("order-log-queue",log);
            }else{//未支付
                Integer teamReservId = orderInfo.getTeamReservId();
                pfc.updateTeamReservState(TeamResevState.CANCEL.getState(), teamReservId);
                orderInfo.setOrderState(OrderState.CANCELED.getState());
                orderService.updateById(orderInfo);
                //记录日志
                OrderLog log = new OrderLog();
                log.setLogUser("消息队列操作");
                log.setLogDesc("未支付，取消了用户订单");
                log.setLogTime(DateUtil.dateTimeToString(new Date()));
                log.setOrderNum(orderNum);
                rabbitTemplate.convertAndSend("order-log-queue",log);
            }
        }
    }
}
